<template>
  <div>
    <span  v-text="totalNum"></span>
    <el-button type="text" icon="el-icon-setting" v-show="is_org_edit" @click="handleUpdate" style="margin-left: 10px;"
    v-hasPermi="['labdata:gasCylinderNum:edit']">设置</el-button>
    <!-- 添加或修改危化气瓶数量对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item v-for='(item, index) in this.gasCylindersTypeOptions' :key="index" :label="item.name" :prop="item.cylindersNum" >
          <el-input type="number" v-model="item.cylindersNum" :placeholder="'请输入'+item.name+'气瓶数量'" size="small" class="room" @input="onInput()"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-hasPermi="['labdata:gasCylinderNum:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAllGasCylindersType,
listGasCylinderNum, addGasCylinderNum,
updateGasCylinderNum } from "@/api/labdata/gasCylinderNum";

export default {
  name: "GasCylinderNumCom",
  data() {
    return {
      is_org_edit: false,
      totalNum: 0,
      // 遮罩层
      loading: true,
      // 危化气瓶数量表格数据
      gasCylinderNumList: [],
      gasCylindersTypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        labId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  props:{
    labId: Number,
    isEdit: Boolean,
  },
  watch: {
    labId:function(newVal, oldVal){
     this.queryParams.labId = newVal;
     this.form.labId = newVal;
     this.getAllGasType();
     this.getList();
    },
    isEdit:function(newVal, oldVal){
      this.is_org_edit = newVal;
    }

  },
  created() {
    this.queryParams.labId = this.$props.labId;
    this.form.labId = this.$props.labId;
    this.getAllGasType();
    this.getList();
  },
  methods: {
    /** 查询危化气瓶数量列表 */
    getList() {
      this.loading = true;
      listGasCylinderNum(this.queryParams).then(response => {
        this.gasCylinderNumList = response.rows;
        this.totalNum = 0;
        this.gasCylinderNumList.forEach(item =>{
           this.totalNum = this.totalNum + item.cylindersNum;
        });
      });
    },
    getAllGasType(){
      getAllGasCylindersType().then(response => {
        this.gasCylindersTypeOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.getList();
      this.open = false;
    },
    onInput(){
         this.$forceUpdate();
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.gasCylinderNumList.forEach(item =>{
        for(let i=0;i<this.gasCylindersTypeOptions.length;i++){
          if(this.gasCylindersTypeOptions[i].cylindersTypeId==item.cylindersTypeId){
            this.gasCylindersTypeOptions[i].cylindersNum = (item.cylindersNum).toString();
            this.gasCylindersTypeOptions[i].cylinderNumberId = item.cylinderNumberId;
          }
        }
      });
      this.open = true;
      this.title = "设置危化气瓶数量";
    },
    /** 提交按钮 */
    submitForm() {
        this.gasCylindersTypeOptions.forEach(item =>{
          item.labId = this.form.labId;
        });
        addGasCylinderNum(this.gasCylindersTypeOptions).then(response => {
          this.msgSuccess("保存成功");
          this.open = false;
          this.getList();
        });
    },

  }
};
</script>
<style type="text/css" scoped>
  .spanspace {
    margin-right: 5px;
  }
</style>
